package com.jiepos.mpos.biz.code.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiepos.mpos.common.constant.DBConstant;
import com.jiepos.mpos.common.constant.DBConstant.Table;
import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.common.dao.InfSuperBankDao;
import com.jiepos.mpos.common.entity.App_Inf_SuperbankEntity;
import com.jiepos.mpos.core.entity.Pagination;
import com.jiepos.mpos.core.service.BaseService;
import com.jiepos.mpos.core.util.IDFactoryUtil;
import com.jiepos.mpos.core.util.StringUtils;

/**
 * 
 * 总行号列表管理
 * @ProjectName:  [mpos] 
 * @Package com.jiepos.mpos.biz.code.service 
 * @author 张磊
 * @version 1.0
 * @since 2017年10月13日 下午2:11:57
 * @history（历次修订内容、修订人、修订时间等）
 * @UpdateRemark [说明本次修改内容]
 * @Copyright (c) 2017 江苏杰博实信息技术有限公司
 */
@Service
public class SuperBankService extends BaseService {

	@Autowired
	private InfSuperBankDao infSuperBankDao;
	
	/**
	 * 获取总行号列表Json
	 * @param pagination
	 * @param appInfSuperbankEntity
	 * @return
	 */
	public Map getSuperBankList(Pagination pagination, App_Inf_SuperbankEntity appInfSuperbankEntity){
		
		StringBuffer sb = this.getInitSql("t1");
		List<String> list = new ArrayList<>();
		sb.append(" ,super_bank_uuid as uuid,super_bank_no,bank_nm,bank_addr,(select code_val ||'-'||code_desc from ")
			.append(Table.T_APP_CODE_LIST).append(" where code_tp = '"+DBConstant.Code.BANK_ISMAKE+"' and code_val = t1.is_make) as is_make from ")
			.append(Table.T_APP_INF_SUPERBANK).append(" t1 where 1=1 ");
		if(!StringUtils.isBlank(appInfSuperbankEntity.getSuper_bank_no())){
			sb.append(" and t1.super_bank_no like ?");
			list.add("%"+appInfSuperbankEntity.getSuper_bank_no()+"%");
		}
		if(!StringUtils.isBlank(appInfSuperbankEntity.getBank_nm())){
			sb.append(" and t1.bank_nm like ?");
			list.add("%"+appInfSuperbankEntity.getBank_nm()+"%");
		}
		if(!StringUtils.isBlank(appInfSuperbankEntity.getBank_addr())){
			sb.append(" and t1.bank_addr like ?");
			list.add("%"+appInfSuperbankEntity.getBank_addr()+"%");
		}
		if(!StringUtils.isBlank(appInfSuperbankEntity.getIs_make())){
			sb.append(" and t1.is_make = ?");
			list.add(appInfSuperbankEntity.getIs_make());
		}
		
		pagination.queryString = sb.toString();
		pagination = this.getOrderSql(pagination, "t1");
		return infSuperBankDao.getPaginationMap(pagination, "t1", list);
	}
	
	/**
	 * 根据id查找总行行号详情
	 * @param super_bank_uuid
	 * @return
	 */
	public App_Inf_SuperbankEntity getSuperBankById(String super_bank_uuid){
		if(StringUtils.isBlank(super_bank_uuid)||"undefined".equals(super_bank_uuid)){
			return new App_Inf_SuperbankEntity();
		}
		return infSuperBankDao.findByParam("super_bank_uuid", super_bank_uuid);
	}
	
	/**
	 * 根据总行号查询总行信息
	 * @param bankNo
	 * @return
	 */
	public App_Inf_SuperbankEntity getSuperBankByNo(String bankNo){
		if(StringUtils.isBlank(bankNo)){
			return new App_Inf_SuperbankEntity();
		}
		return infSuperBankDao.findByParam("super_bank_no", bankNo);
	}
	
	/**
	 * 验证总行行号是否唯一
	 * @param app_Inf_SuperbankEntity
	 * @return
	 */
	public String checkSuperbank(App_Inf_SuperbankEntity app_Inf_SuperbankEntity){
		StringBuffer sb = this.getInitSql("t1").append(",");
		sb.append("super_bank_uuid as uuid,super_bank_no,bank_nm,bank_addr from ");
		sb.append(Table.T_APP_INF_SUPERBANK);
		sb.append(" t1 where 1=1 ");
		List<String> list = new ArrayList<>();
		if(!StringUtils.isBlank(app_Inf_SuperbankEntity.getSuper_bank_no())){
			sb.append(" and t1.super_bank_no = ?");
			list.add(app_Inf_SuperbankEntity.getSuper_bank_no());
		}
		if(!StringUtils.isBlank(app_Inf_SuperbankEntity.getSuper_bank_uuid())){
			sb.append(" and t1.super_bank_uuid != ?");
			list.add(app_Inf_SuperbankEntity.getSuper_bank_uuid());
		}
		List<App_Inf_SuperbankEntity> result = infSuperBankDao.queryForList(sb.toString(), list);
		if(result.size()==0||result == null){
			return SystemConstant.SUCCESS;
		}
		return SystemConstant.ERROR;
	}
	
	/**
	 * 保存总行行号信息
	 * @param app_Inf_SuperbankEntity
	 * @param map
	 */
	public void saveSuperBank(App_Inf_SuperbankEntity app_Inf_SuperbankEntity,Map map){
		if(!StringUtils.isBlank(app_Inf_SuperbankEntity.getSuper_bank_uuid())){
			App_Inf_SuperbankEntity newApp_Inf_SuperbankEntity = infSuperBankDao.findByParam("super_bank_uuid", app_Inf_SuperbankEntity.getSuper_bank_uuid());
			newApp_Inf_SuperbankEntity = (App_Inf_SuperbankEntity) this.saveCommonData(newApp_Inf_SuperbankEntity, map, "update");
			newApp_Inf_SuperbankEntity.setIs_make(app_Inf_SuperbankEntity.getIs_make());
			newApp_Inf_SuperbankEntity.setBank_addr(app_Inf_SuperbankEntity.getBank_addr());
			newApp_Inf_SuperbankEntity.setBank_nm(app_Inf_SuperbankEntity.getBank_nm());
			newApp_Inf_SuperbankEntity.setSuper_bank_no(app_Inf_SuperbankEntity.getSuper_bank_no());
			List<String> noChangeList = new ArrayList<>();
			noChangeList.add("super_bank_uuid");
			noChangeList.add("rec_crt_ts");
			infSuperBankDao.update(newApp_Inf_SuperbankEntity, noChangeList);
		}else {
			app_Inf_SuperbankEntity.setSuper_bank_uuid(IDFactoryUtil.getUUID());
			app_Inf_SuperbankEntity = (App_Inf_SuperbankEntity) this.saveCommonData(app_Inf_SuperbankEntity, map, "");
			infSuperBankDao.save(app_Inf_SuperbankEntity);
		}
	}
	
	/**
	 * 删除总行行号
	 * @param super_bank_uuids
	 */
	public void deleteSuperBankByIds(String super_bank_uuids){
		infSuperBankDao.deleteByStatus("super_bank_uuid",super_bank_uuids);
	}
	
	/**
	 * 获取总行行号
	 * @return
	 */
	public List getSuperBankInfo(){
		
		StringBuffer sufsql = new StringBuffer();
		List<String> list = new ArrayList<>();
		sufsql.append("select super_bank_no as \"super_bank_no\",bank_nm as \"super_bank_nm\",bank_addr as \"super_bank_addr\" from ")
				.append(Table.T_APP_INF_SUPERBANK).append(" where is_make = ? and rec_st != ?");
		list.add(SystemConstant.IS_MAKE_1);
		list.add(SystemConstant.REC_ST_0);
		List<Map<String, Object>> result = infSuperBankDao.queryForList(sufsql.toString(), list);
		
		return result;
	}
}
